const WXAPI = require('../../wxapi/main')
const app = getApp();
const WxParse = require('../../wxParse/wxParse.js');
const regeneratorRuntime = require('../../utils/runtime')

Page({
  data: {
    dish: [
      [[{
        did:0,
        mid:0,
        cid:0,
        dName:"麻婆豆腐",
        dPrice:10,
        dImage:"/images/merchants/merchant1/category1/df.png",
      },{
        did:1,
        mid:0,
        cid:0,
        dName:"红烧冬瓜",
        dPrice:9,
        dImage:"/images/merchants/merchant1/category1/dg.png",
      },{
        did:2,
        mid:0,
        cid:0,
        dName:"手撕杏鲍菇",
        dPrice:9,
        dImage:"/images/merchants/merchant1/category1/xbg.png",
      }]
      ,[{
        did:10,
        mid:0,
        cid:1,
        dName:"甜艾滚蛋",
        dPrice:5,
        dImage:"/images/merchants/merchant1/category2/a.png",
      },{
        did:11,
        mid:0,
        cid:1,
        dName:"虾仁菌菇汤",
        dPrice:5,
        dImage:"/images/merchants/merchant1/category2/xr.jpg",
      }],
      [{
        did:20,
        mid:0,
        cid:2,
        dName:"米饭",
        dPrice:1,
        dImage:"/images/merchants/merchant1/category3/mf.png",
      }]],
      [[{
        did:100,
        mid:1,
        cid:0,
        dName:"臊子面",
        dPrice:10,
        dImage:"/images/merchants/merchant2/category1/sz.jpg",
      },
      {
        did:101,
        mid:1,
        cid:0,
        dName:"西红柿鸡蛋面",
        dPrice:10,
        dImage:"/images/merchants/merchant2/category1/xhs.jpg",
      },{
        did:102,
        mid:1,
        cid:0,
        dName:"意大利面",
        dPrice:10,
        dImage:"/images/merchants/merchant2/category1/ydl.jpg",
      }],
      [{
        did:110,
        mid:1,
        cid:1,
        dName:"酸梅汤",
        dPrice:7,
        dImage:"/images/merchants/merchant2/category2/sm.jpg",
      },
      {
        did:111,
        mid:1,
        cid:1,
        dName:"西瓜汁",
        dPrice:7,
        dImage:"/images/merchants/merchant2/category2/xg.jpg",
      }]],

      [[{
        did:200,
        mid:2,
        cid:0,
        dName:"手撕鸡肉",
        dPrice:16,
        dImage:"/images/merchants/merchant3/category1/jr.jpg",
      },{
        did:201,
        mid:2,
        cid:0,
        dName:"牛肉拌香菜",
        dPrice:19,
        dImage:"/images/merchants/merchant3/category1/nr.jpg",
      },{
        did:202,
        mid:2,
        cid:0,
        dName:"酱肘子",
        dPrice:20,
        dImage:"/images/merchants/merchant3/category1/zj.jpg",
      }],
      [{
        did:210,
        mid:2,
        cid:1,
        dName:"小葱拌豆腐",
        dPrice:8,
        dImage:"/images/merchants/merchant3/category2/df.jpg",
      },{
        did:211,
        mid:2,
        cid:1,
        dName:"酸辣凉粉",
        dPrice:12,
        dImage:"/images/merchants/merchant3/category2/lf.jpg",
      }]],
      [[{
        did:300,
        mid:3,
        cid:0,
        dName:"吐司",
        dPrice:10,
        dImage:"/images/merchants/merchant4/category1/ts.jpg",
      },{
        did:301,
        mid:3,
        cid:0,
        dName:"枣沙面包",
        dPrice:10,
        dImage:"/images/merchants/merchant4/category1/zs.jpg",
      }],
      [{
        did:310,
        mid:3,
        cid:1,
        dName:"水果披萨",
        dPrice:15,
        dImage:"/images/merchants/merchant4/category2/sg.jpg",
      },{
        did:311,
        mid:3,
        cid:1,
        dName:"吐司披萨",
        dPrice:15,
        dImage:"/images/merchants/merchant4/category2/ts.jpg",
      }]]],

    goodsDetail: {
      basicInfo:{},
      // properties:{}
    },
    selectSizePrice: 0,
    shopNum: 0, // 购物车中商品数量
    buyNumber: 0,
    buyNumMin: 1,
    buyNumMax: 0,

    propertyChildIds: "",
    propertyChildNames: "",
    canSubmit: false, //  选中规格尺寸时候是否允许加入购物车
    shopCarInfo: {}, 
    currentPages: undefined
  },
  
  async onLoad(e) {
    console.log(e)
    this.data.goodsId = e.id // e.id = did
    const that = this
    this.data.kjJoinUid = e.kjJoinUid
    
    // 获取购物车数据
    wx.getStorage({
      key: 'shopCarInfo',
      success: function(res) {
        that.setData({
          shopCarInfo: res.data,
          shopNum: res.data.shopNum,
          curuid: wx.getStorageSync('uid')
        });
      }
    })
  },

  onShow (){
    // 剪贴板数据
    wx.setClipboardData({
      data: 'data',
      success(res) {
        wx.getClipboardData({
          success(res) {
            console.log("res.data", res.data) // data
          }
        })
      }
    })

    wx.getClipboardData({
      success(res) {
        console.log("res.data",res.data)
      }
    })

    // 获取详细菜品数据（新）
    console.log("this.data.goodsId", this.data.goodsId);
    this.getGoodsDetail(this.data.goodsId);

    // 获取详细菜品数据（旧）
  },

  async getGoodsDetail(goodsId) {
    let that = this;

    let mid = parseInt(goodsId/100);
    let cid = parseInt(goodsId/10) - 10*mid;
    let did = goodsId - 100*mid - 10*cid;

    let goodsDetail = {
      basicInfo:{},
      // properties:{}
    };

    goodsDetail.basicInfo = that.data.dish[mid][cid][did];
    
    // console.log("goodsDetail.basicInfo", goodsDetail.basicInfo);

    // goodsDetail.basicInfo = basicInfo;
  
    that.setData({
      // 菜品属性
      goodsDetail: goodsDetail,
      selectSizePrice: goodsDetail.basicInfo.dPrice,
      buyNumMax: 1000, // 设置为1000
      buyNumber: 1, // 不判断库存
      currentPages: getCurrentPages()
      // 
    });

    console.log("that.data.goodsDetail", that.data.goodsDetail);


      
    // WxParse.wxParse('article', 'html', goodsDetailRes.data.content, that, 5);

  },

  // 获取详细菜品数据（旧）
  async getGoodsDetailAndKanjieInfo(goodsId) {
    const that = this;
    
    // 菜品数据
    const goodsDetailRes = await WXAPI.goodsDetail(goodsId)
    // 砍价

    if (goodsDetailRes.code == 0) {
      if (goodsDetailRes.data.properties) {
        that.setData({
          selectSizePrice: goodsDetailRes.data.basicInfo.minPrice,
        });
      }
      
      // 拼团

      // 商品数据
      that.data.goodsDetail = goodsDetailRes.data;

      let _data = {
        goodsDetail: goodsDetailRes.data,
        selectSizePrice: goodsDetailRes.data.basicInfo.minPrice,
        buyNumMax: goodsDetailRes.data.basicInfo.stores,
        buyNumber: (goodsDetailRes.data.basicInfo.stores > 0) ? 1 : 0,
        currentPages: getCurrentPages()
      }

      // 砍价


      // 拼团

      // 存储数据
      that.setData(_data);
      WxParse.wxParse('article', 'html', goodsDetailRes.data.content, that, 5);
    }
  },

  // 进入购物车
  goShopCar: function() {
    wx.reLaunch({
      url: "/pages/shop-cart/index"
    });
  },

  // 减数量
  numJianTap: function() {
    if (this.data.buyNumber > this.data.buyNumMin) {
      var currentNum = this.data.buyNumber;
      currentNum--;
      this.setData({
        buyNumber: currentNum
      })
    }
  },
  // 加数量
  numJiaTap: function() {
    if (this.data.buyNumber < this.data.buyNumMax) {
      var currentNum = this.data.buyNumber;
      currentNum++;
      this.setData({
        buyNumber: currentNum
      })
    }
  },

  /**
   * 选择商品规格（可取消）
   * @param {Object} e
   */
  /*
  labelItemTap: function(e) {
    const that = this;
    // 取消该分类下的子栏目所有的选中状态
    var childs = that.data.goodsDetail.properties[e.currentTarget.dataset.propertyindex].childsCurGoods;
    for (var i = 0; i < childs.length; i++) {
      that.data.goodsDetail.properties[e.currentTarget.dataset.propertyindex].childsCurGoods[i].active = false;
    }
    // 设置当前选中状态
    that.data.goodsDetail.properties[e.currentTarget.dataset.propertyindex].childsCurGoods[e.currentTarget.dataset.propertychildindex].active = true;
    // 获取所有的选中规格尺寸数据
    var needSelectNum = that.data.goodsDetail.properties.length;
    var curSelectNum = 0;
    var propertyChildIds = "";
    var propertyChildNames = "";
    for (var i = 0; i < that.data.goodsDetail.properties.length; i++) {
      childs = that.data.goodsDetail.properties[i].childsCurGoods;
      for (var j = 0; j < childs.length; j++) {
        if (childs[j].active) {
          curSelectNum++;
          propertyChildIds = propertyChildIds + that.data.goodsDetail.properties[i].id + ":" + childs[j].id + ",";
          propertyChildNames = propertyChildNames + that.data.goodsDetail.properties[i].name + ":" + childs[j].name + "  ";
        }
      }
    }

    var canSubmit = false;
    if (needSelectNum == curSelectNum) {
      canSubmit = true;
    }

    // 计算当前价格
    if (canSubmit) {
      WXAPI.goodsPrice({
        goodsId: that.data.goodsDetail.basicInfo.id,
        propertyChildIds: propertyChildIds
      }).then(function(res) {
        that.setData({
          selectSizePrice: res.data.price,
          propertyChildIds: propertyChildIds,
          propertyChildNames: propertyChildNames,
          buyNumMax: res.data.stores,
          buyNumber: (res.data.stores > 0) ? 1 : 0
        });
      })
    }


    this.setData({
      goodsDetail: that.data.goodsDetail,
      canSubmit: canSubmit
    })
  },
  */

  /**
   * 加入购物车
   */
  addShopCar: function() {

    // if (this.data.goodsDetail.properties && !this.data.canSubmit) {
    //   if (!this.data.canSubmit) {
    //     wx.showModal({
    //       content: '请选择商品具体规格',
    //       showCancel: false
    //     })
    //   }
    //   return;
    // }


    if (this.data.buyNumber < 1) {
      wx.showModal({
        title: '提示',
        content: '购买数量不能为0！',
        showCancel: false
      })
      return;
    }

    //组建购物车
    var shopCarInfo = this.bulidShopCarInfo();

    this.setData({
      shopCarInfo: shopCarInfo,
      shopNum: shopCarInfo.shopNum
    });

    // 写入本地存储
    wx.setStorage({
      key: 'shopCarInfo',
      data: shopCarInfo
    });

    wx.showToast({
      title: '加入购物车成功',
      icon: 'success'
    })
  },


  /**
   * 组建购物车信息
   */
  bulidShopCarInfo: function() {
    // 加入购物车
    var shopCarMap = {};
    shopCarMap.goodsId = this.data.goodsDetail.basicInfo.did;
    shopCarMap.pic = this.data.goodsDetail.basicInfo.dImage;
    shopCarMap.name = this.data.goodsDetail.basicInfo.dName;

    // shopCarMap.propertyChildIds = this.data.propertyChildIds;
    // shopCarMap.label = this.data.propertyChildNames;

    shopCarMap.price = this.data.selectSizePrice;

    shopCarMap.left = "";
    shopCarMap.active = true;
    shopCarMap.number = this.data.buyNumber;

    // shopCarMap.logisticsType = this.data.goodsDetail.basicInfo.logisticsId;
    // shopCarMap.logistics = this.data.goodsDetail.logistics;
    // shopCarMap.weight = this.data.goodsDetail.basicInfo.weight;

    // 购物车信息
    var shopCarInfo = this.data.shopCarInfo;
    if (!shopCarInfo.shopNum) {
      shopCarInfo.shopNum = 0;
    }
    // 购物车列表
    if (!shopCarInfo.shopList) {
      shopCarInfo.shopList = [];
    }

    var hasSameGoodsIndex = -1;
    for (var i = 0; i < shopCarInfo.shopList.length; i++) {
      var tmpShopCarMap = shopCarInfo.shopList[i];
      if (tmpShopCarMap.goodsId == shopCarMap.goodsId) {
        hasSameGoodsIndex = i;
        shopCarMap.number = shopCarMap.number + tmpShopCarMap.number;
        break;
      }
    }

    shopCarInfo.shopNum = shopCarInfo.shopNum + this.data.buyNumber;
    if (hasSameGoodsIndex > -1) {
      shopCarInfo.shopList.splice(hasSameGoodsIndex, 1, shopCarMap);
    } else {
      shopCarInfo.shopList.push(shopCarMap);
    }
    shopCarInfo.kjId = this.data.kjId;
    return shopCarInfo;
  },


  /*
  onShareAppMessage: function() {
    let _data = {
      title: this.data.goodsDetail.basicInfo.name,
      path: '/pages/goods-details/index?id=' + this.data.goodsDetail.basicInfo.id + '&inviter_id=' + wx.getStorageSync('uid'),
      success: function(res) {
        // 转发成功
      },
      fail: function(res) {
        // 转发失败
      }
    }
    if (this.data.kjJoinUid) {
      _data.title = this.data.curKanjiaprogress.joiner.nick + '邀请您帮TA砍价'
      _data.path += '&kjJoinUid=' + this.data.kjJoinUid
    }
    return _data
  },
*/

  goIndex() {
    wx.switchTab({
      url: '/pages/index/index',
    });
  }
})
